package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.template.DestinationTemplate;
import cn.wolfcode.luowowo.search.template.StrategyTemplate;
import cn.wolfcode.luowowo.search.template.TravelTemplate;
import cn.wolfcode.luowowo.search.template.UserInfoTemplate;
import cn.wolfcode.luowowo.search.vo.SearchResultVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class SearchController {

    @Reference
    private IDestinationTemplateSearchService destinationSearchService;

    @Reference
    private IStrategyTemplateSearchService strategySearchService;

    @Reference
    private ITravelTemplateSearchService travelSearchService;

    @Reference
    private IUserInfoTemplateSearchService userInfoSearchService;

    @Reference
    private ISearchService searchService;


    @RequestMapping("/q")
    public String query(Model model, @ModelAttribute("qo") SearchQueryObject qo) {
        switch (qo.getType()) {
            case SearchQueryObject.TYPE_DEST:
                return searchDest(model, qo);
            case SearchQueryObject.TYPE_STRATEGY:
                return searchStrategy(model, qo);
            case SearchQueryObject.TYPE_TRAVEL:
                return searchTravel(model, qo);
            case SearchQueryObject.TYPE_USER:
                return searchUser(model, qo);
            default:
                return searchAll(model, qo);
        }
    }

    //查询所有
    private String searchAll(Model model, SearchQueryObject qo) {
        //高亮显示操作
        Page ss = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME,
                StrategyTemplate.class, qo, "title", "subTitle", "summary");
        Page us = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME,UserInfoTemplate.TYPE_NAME,
                UserInfoTemplate.class,qo,"nickname","destName");
        Page ts = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME,TravelTemplate.TYPE_NAME,
                TravelTemplate.class,qo,"title","summary");
        Page ds = searchService.searchWithHighlight(DestinationTemplate.INDEX_NAME,DestinationTemplate.TYPE_NAME,
                DestinationTemplate.class,qo,"name","info");

        SearchResultVO vo = new SearchResultVO();
        vo.setUsers(us.getContent());
        vo.setTravels(ts.getContent());
        vo.setDests(ds.getContent());
        vo.setStrategys(ss.getContent());
        vo.setTotal(ds.getTotalElements() + ss.getTotalElements()+us.getTotalElements()+ts.getTotalElements());

        model.addAttribute("data",vo);

        return "index/searchAll";
    }


    //用户查询
    private String searchUser(Model model, SearchQueryObject qo) {
        //高亮显示操作
        Page page = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME,UserInfoTemplate.TYPE_NAME,
                UserInfoTemplate.class,qo,"nickname","destName");
        model.addAttribute("page",page);
        return "index/searchUser";
    }


    //游记查询
    private String searchTravel(Model model, SearchQueryObject qo) {

        //高亮显示操作
        Page page = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME,TravelTemplate.TYPE_NAME,
                TravelTemplate.class,qo,"title","summary");
        model.addAttribute("page",page);
        return "index/searchTravel";
    }


    //目的地查询
    private String searchDest(Model model, SearchQueryObject qo) {
        //1.通过关键字查询目的地对象
        DestinationTemplate destination = destinationSearchService.findByName(qo.getKeyword());
        //2.判断是否为空,如果存在,查询该目的地下所有攻略  游记  用户(页面效果:取前五个)
        if (destination != null) {
            long total = 0;
            //攻略
            List<StrategyTemplate> sts = strategySearchService.findByDestName(qo.getKeyword());
            total += sts.size();
            if (sts.size() > 5) {
                sts.subList(0, 5);
            }

            //游记
            List<TravelTemplate> ts = travelSearchService.findByDestName(qo.getKeyword());
            total += ts.size();
            if (ts.size() > 5) {
                ts.subList(0, 5);
            }

            //用户
            List<UserInfoTemplate> us = userInfoSearchService.findByDestName(qo.getKeyword());
            total += us.size();
            if (us.size() > 5) {
                us.subList(0, 5);
            }

            SearchResultVO data = new SearchResultVO();
            data.setStrategys(sts);
            data.setTotal(total);
            data.setTravels(ts);
            data.setUsers(us);
            model.addAttribute("data", data);
            model.addAttribute("dest", destination);
        }
        return "index/searchDest";
    }

    //攻略查询
    private String searchStrategy(Model model, SearchQueryObject qo) {
        //高亮显示操作
        Page page = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME,
                StrategyTemplate.class, qo, "title", "subTitle", "summary");
        model.addAttribute("page", page);
        return "index/searchStrategy";
    }
}
